﻿using FPSO.Models.Constant;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;
using static FPSO.Models.Constant.TankConstant;

namespace FPSO.HMI.Views.Dialog
{
    /// <summary>
    /// WBTTaskDialog.xaml 的交互逻辑
    /// </summary>
	/// NOTICE 现在暂时只进行压载舱倒舱操作
    public partial class COTLoadTaskDialog : Window
    {
		private Dictionary<string, double> localVolmDic = new Dictionary<string, double>();
		//g_Adjustvol记录需调载的压载水体积，缺省使用100方
		public string g_TargetTank = string.Empty;
		public double g_Adjustvol = 100;
		private bool l_TankType = false;
		//g_LoadType = 1是装载或注入，2是卸载或外输
		public int g_LoadType = 1;
		/// <summary>
		/// 
		/// </summary>
		/// <param name="tankName"></param>
		/// <param name="currentVolmDic"></param>
		/// <param name="tankType">false 货油舱，true 压载舱</param>
		public COTLoadTaskDialog(string tankName, Dictionary<string, double> currentVolmDic,bool tankType)
        {
            InitializeComponent();

			localVolmDic = currentVolmDic;

			l_TankType = tankType;

			if (tankType == false)
			{
				DialogTitle.Text = "货油舱调载计划";
				cmbTaskType.Items.Add(new ComboBoxItem() { Content = TaskTypeConstant.LOAD_NAME });
			}
			else
			{
				DialogTitle.Text = "压载舱调载计划";
				cmbTaskType.Items.Add(new ComboBoxItem() { Content = TaskTypeConstant.BALLAST_NAME });
				cmbTaskType.Items.Add(new ComboBoxItem() { Content = TaskTypeConstant.UNBALLAST_NAME });
			}
			cmbTaskType.SelectedIndex = 0;

			cmbSourceCOT.Text = tankName;
			string tankid = TankConstant.GetTankId(tankName);

			double srctankvol = localVolmDic[tankid];
			txbCOTInitFill.Text = string.Format("{0:N2}", srctankvol);
			txbCOTTargetFill.Text = string.Format("{0:N2}", g_Adjustvol); ;

			SetAdjustVols();

		}
        private void Confirm_Click(object sender, RoutedEventArgs e)
        {
            try
            {
				SetAdjustVols();
				if (txbCOTTargetFill.Text == null)
				{
					throw new Exception("请输入舱室调载量!");
				}
				//这里输出g_TargetTank，g_Adjustvol，如果不确认，则清空这两个数据
				this.DialogResult = true;
				this.Close();
			}
            catch(Exception ex)
            {
                MessageBox.Show(ex.Message, "错误", MessageBoxButton.OK, MessageBoxImage.Error);
            }
        }

        private void Cancel_Click(object sender, RoutedEventArgs e)
        {
			g_TargetTank = string.Empty;
			g_Adjustvol = 0;
			this.DialogResult = false;
			this.Close();
        }

        private void Window_Loaded(object sender, RoutedEventArgs e)
        {
		}

		private void WindowMove(object sender, MouseEventArgs e)
		{
			if (e.LeftButton == MouseButtonState.Pressed)
			{
				DragMove();
			}
		}
		private void SetAdjustVols()
		{
			g_Adjustvol = Convert.ToDouble(txbCOTTargetFill.Text);
			string SrcTank = cmbSourceCOT.Text;
			List<ConstantTankInfo> taskInfos = TankConstant.getCOTTank();
			if(l_TankType == true)
				taskInfos = TankConstant.getWBTTank();
			foreach (ConstantTankInfo info in taskInfos)
			{
				string tankname0 = info.TankName;
				if (tankname0 == SrcTank)
				{
					string tankid0 = info.TankID;
					double current_vol = localVolmDic[tankid0];
					double total_vol = info.TankVolume;
					txbCOTInitFill.Text = string.Format("{0:N2}", current_vol);
					double res_vol = total_vol - current_vol;
					//装载或注入
					double tarvol = 0;
					if (cmbTaskType.SelectedIndex == 0)
					{
						if (res_vol < g_Adjustvol)
							g_Adjustvol = res_vol;
						tarvol = current_vol + g_Adjustvol;
						g_LoadType = 1;
					}
					else//卸载或外输
					{
						if (current_vol < g_Adjustvol)
							g_Adjustvol = current_vol;
						tarvol = current_vol - g_Adjustvol;
						g_LoadType = 2;
					}
					txbCOTTargetFill.Text = string.Format("{0:N2}", g_Adjustvol);
					txbTarCOTTargetFill.Text = string.Format("{0:N2}", tarvol);
					break;
				}
			}
		}
		private void txbCOTTargetFill_LostFocus(object sender, RoutedEventArgs e)
		{
			SetAdjustVols();
		}

		private void cmbTaskType_SelectionChanged(object sender, SelectionChangedEventArgs e)
		{
			//SetAdjustVols();
		}
	}
}
